﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using blackhouse.BlackApart.Business;
using blackhouse.Currencies;
using System.Configuration;
using blackhouse.JavaScript.JQuery;

namespace blackhouse.BlackApart.Web.Modules {
	public partial class CurrencyLiteral : System.Web.UI.UserControl {

		#region Fields

		private DisplayCurrency currency = new DisplayCurrency();

		//private CultureInfo currencyLanguage;

		//private string currency;

		//private const string CURRENCY_KEY = "Currency";

		#endregion

		#region Properties

		//public string Currency {
		//    get {
		//        if (!string.IsNullOrEmpty(this.currency))
		//            return currency;

		//        this.currency = this.GetCurrencyFromCookie();
		//        if (!string.IsNullOrEmpty(this.currency))
		//            return currency;

		//        this.currency = this.GetCurrencyFromUserLanguage();
		//        if (!string.IsNullOrEmpty(this.currency))
		//            return currency;

		//        this.currency = Factory.Instance().GetCompany().GetCurrentUserCurrency().Code.ISOCode;
		//        return this.currency;
		//    }

		//    set {
		//        if (Currencies.Currency.Verify(value)) {
		//            this.currency = value;
		//            this.SetCurrencyToCookie(value);
		//        }
		//    }

		//}

		public double Value { get; set; }

		public int Precision { get; set; }

		public bool DisableChangeCurrency { get; set; }

		#endregion

		#region Methods

		//private string GetCurrencyFromUserLanguage() {
		//    string[] languages = HttpContext.Current.Request.UserLanguages;
		//    for (int xl = 0; xl < languages.Length; xl++) {
		//        string language = languages[xl].Split(';')[0];
		//        CultureInfo ci = new CultureInfo(language);
		//        if (!ci.IsNeutralCulture) {
		//            Currencies.Currency c = Currencies.Currency.FromCultureInfo(ci);
		//            if (c != null) return c.Code.ISOCode;
		//        }
		//    }
		//    return "";
		//}

		//private void SetCurrencyToCookie(string currencyCode) {
		//    HttpCookie cook = Request.Cookies[CURRENCY_KEY];
		//    if (cook == null || cook.Value != currencyCode) {
		//        cook = Response.Cookies[CURRENCY_KEY];
		//        if (cook == null) Response.Cookies.Add(new HttpCookie(CURRENCY_KEY, currencyCode));
		//        else Response.Cookies[CURRENCY_KEY].Value = currencyCode;
		//    }
		//}

		//private string GetCurrencyFromCookie() {
		//    HttpCookie cook = Request.Cookies[CURRENCY_KEY];
		//    if (cook != null && Currencies.Currency.Verify(cook.Value)) {
		//        return cook.Value;
		//    }
		//    else return "";
		//}

		//public static string ReadUserCurrencyFromCookie() {
		//    HttpCookie cook = HttpContext.Current.Request.Cookies[CURRENCY_KEY];
		//    if (cook != null && Currencies.Currency.Verify(cook.Value))
		//        return cook.Value;
		//    else
		//        return "";
		//}

		private void ReceiveRequest() {

			if (this.IsPostBack && !string.IsNullOrEmpty(Request.Form["SetCurrency"]) && this.currency.ReceiveRequest(Request.Form["SetCurrency"]))
				Response.Redirect(Request.RawUrl, true);

			//if (this.IsPostBack && !string.IsNullOrEmpty(Request.Form["SetCurrency"])) {
			//    string newCurrency = Request.Form["SetCurrency"];
			//    if (!string.IsNullOrEmpty(newCurrency)) {
			//        if (Currencies.Currency.Verify(newCurrency))
			//            this.Currency = newCurrency;
			//        Response.Redirect(Request.RawUrl);
			//    }
			//}
		}

		protected override void OnInit(EventArgs e) {
			this.ReceiveRequest();

			base.OnInit(e);
		}

		protected override void OnLoad(EventArgs e) {
			base.OnLoad(e);

			this.AddJsFiles();

			this.currency = new DisplayCurrency(this.Value, this.Precision);
			this.currency.Load();
			this.lblSelectedCurrency.Text = "<span>" + this.currency.Currency + "</span>";
			this.controlPanel.Visible = !this.DisableChangeCurrency;
			//Currency c = new Currency(this.Value, "PLN", this.Precision);
			//string toC = "PLN";
			//if (CultureInfo.CurrentCulture.Name != "pl-PL")
			//    toC = "EUR";
			//Currency selectedCurrency = c.ToCurrency(this.Currency, toC);
			//this.lblSelectedCurrency.Text = "<span>" + selectedCurrency + "</span>";

			//List<string> currencies = null;
			//string cnfCurrencies = ConfigurationManager.AppSettings["Currencies"];
			//if (!string.IsNullOrEmpty(cnfCurrencies))
			//    currencies = cnfCurrencies.Split(',').ToList();
			//else
			//    currencies = c.GetSupportedCurrencies().ToList();

			//if (!currencies.Contains(selectedCurrency.Code.ISOCode))
			//    currencies.Add(selectedCurrency.Code.ISOCode);

			//var qToRpt = from cur in currencies select new { Currency = c.ToCurrency(cur), Code = cur };
			this.rptCurrencies.DataSource = this.currency.GetCurrencies();
			this.rptCurrencies.DataBind();

			//this.Currency = selectedCurrency.Code.ISOCode;
		}

		private void AddJsFiles() {
			JQueryLibraries jql = new JQueryLibraries();
			jql.IncludeFileBasedOnJQuery("blackhouse.BlackApart.Web.JavaScript.currency.js,blackhouse.BlackApart.Web");
		}

		#endregion

	}
}